import Mock from 'mockjs';

//登录 模拟一个用户集合

const users = [
    {
        id: 1,
        username: 'admin',
        password: 'admin',
        type: 'admin'
    },

    {
        id: 2,
        username: 'guest1',
        password: '111111',
        type: 'guest'
    },
    {
        id: 3,
        username: 'guest2',
        password: '222222',
        type: 'guest'
    },
]

Mock.mock('/user/login', ({ body }) => {
    var user = JSON.parse(body)
    console.log("mock>>>>2222>>>>" + JSON.stringify(user))
    //登录逻辑
    // var isIncloud = users.some(u => u.username == user.username && u.password == user.password)
    let loginUser = users.filter((u) => {
        return u.username == user.username && u.password == user.password
    })
    console.log(loginUser)
    if (loginUser.length > 0) {
        console.log("mock>>>>44444>>>>code: 200,")
        return { //返回给login页面  then(res 
            code: 200,
            msg: '登录成功',
            result: loginUser
        }
    } else {
        console.log("mock>>>>>44444>>>code: 200,")
        return {
            code: -1,
            msg: '登录失败 用户名密码错误'
        }
    }
}

);


Mock.mock('/user/getUserList', () => {
    console.log(222222)

    return {
        code: 200,
        msg: "查询成功",
        result: users
    }

}
);

Mock.mock('/user/addUser', ({ body }) => {
    var user = JSON.parse(body)
    users.push(user)
    return {
        code: 200,
        msg: "添加成功",
        result: users
    }

}
);

Mock.mock('/user/deleteUser', ({ body }) => {
    console.log("mock")
    console.log(body)
    for (let i = 0; i < users.length; i++) {
        if (users[i].id == body) {
            //从数组中删除
            users.splice(0, 1)
        }
    }
    console.log(users)
    return {
        code: 200,
        msg: "删除成功"

    }

}
);



Mock.mock('/user/EidtUser', ({ body }) => {

    let user = JSON.parse(body) //字符串装成对象
    for (let i = 0; i < users.length; i++) {
        if (users[i].id == user.id) {
            users[i] = user
        }
    }
    return {
        code: 200,
        msg: "修改成功"

    }

}
)